package com.wan.controller;

import com.wan.entity.GameHero;
import com.wan.service.GameHeroService;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.thymeleaf.spring5.SpringTemplateEngine;

import java.io.IOException;
import java.util.Date;

@Controller
public class TestController {
    @Autowired
    private SpringTemplateEngine templateEngine;
    @Autowired
    private GameHeroService gameHeroService;



    @RequestMapping("/test/test")
    @ResponseBody
    public String testTest() throws IOException {
        Document document = Jsoup.connect("http://lol.uuu9.com/hero.shtml").get();
        Elements elements = document.select("#champion_list > li > a > img");
        Elements elements1 = document.select("#champion_list > li > span");




        for (Element element : elements1){
            GameHero gameHero = new GameHero();
            String text = element.text();
            gameHero.setHeroName(text);
            gameHeroService.save(gameHero);
        }
        for (Element element1 : elements) {
            String imgHref = element1.attr("src");
            GameHero gameHero = new GameHero();
            gameHero.setHeroImg(imgHref);
            gameHero.setCreateTime(new Date());
            gameHeroService.save(gameHero);
        }
        return "成功";
    }
}
